﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections;
using System.Configuration;

/// <summary>
/// Summary description for quickwheelz
/// </summary>
[DataObject(true)]
public static class quickwheelz
{
  
    [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataSet getVehicles()
    {
        string queryString = "sp_getAllCustomers";
        SqlCommand selectCmd = new SqlCommand(queryString, new SqlConnection(ConfigurationManager.ConnectionStrings["QuickWheelzConnectionString"].ConnectionString));

        selectCmd.CommandType = CommandType.StoredProcedure;

        selectCmd.Connection.Open();
        SqlDataReader resultSet;

        resultSet = selectCmd.ExecuteReader();

        DataSet resultDataSet = new DataSet();

        DataTable resultDataTable = new DataTable();
        DataReaderAdapter da = new DataReaderAdapter();

        da.FillFromReader(resultDataTable, resultSet);
        resultDataSet.Tables.Add(resultDataTable);

        return resultDataSet;
    }

    [DataObjectMethod(DataObjectMethodType.Update)]
    public static int updateVehicle(string vinId, string make, string model, int manYear, string colour)
    {
        string queryString = "sp_updateVehicle";
        SqlCommand selectCmd = new SqlCommand(queryString, new SqlConnection(ConfigurationManager.ConnectionStrings["QuickWheelzConnectionString"].ConnectionString));
        
        selectCmd.CommandType = CommandType.StoredProcedure;

        selectCmd.Parameters.AddWithValue("vinId", vinId);
        selectCmd.Parameters.AddWithValue("make", make);
        selectCmd.Parameters.AddWithValue("model", model);
        selectCmd.Parameters.AddWithValue("manYear", manYear);
        selectCmd.Parameters.AddWithValue("colour", colour);

        selectCmd.Connection.Open();

        int result = selectCmd.ExecuteNonQuery();

        if (result == 1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    [DataObjectMethod(DataObjectMethodType.Delete)]
    public static int deleteVehicle(string vinId)
    {
        string queryString = "sp_deleteVehicle";
        SqlCommand selectCmd = new SqlCommand(queryString, new SqlConnection(ConfigurationManager.ConnectionStrings["QuickWheelzConnectionString"].ConnectionString));

        selectCmd.CommandType = CommandType.StoredProcedure;

        selectCmd.Parameters.AddWithValue("vinId", vinId);

        selectCmd.Connection.Open();

        int result = selectCmd.ExecuteNonQuery();

        if (result == 1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataSet getRentals()
    {
        string queryString = "sp_getAllRentals";
        SqlCommand selectCmd = new SqlCommand(queryString, new SqlConnection(ConfigurationManager.ConnectionStrings["QuickWheelzConnectionString"].ConnectionString));

        selectCmd.Connection.Open();
        SqlDataReader resultSet;

        resultSet = selectCmd.ExecuteReader();

        DataSet resultDataSet = new DataSet();

        DataTable resultDataTable = new DataTable(); 
        DataReaderAdapter da = new DataReaderAdapter(); 

        da.FillFromReader(resultDataTable, resultSet);
        resultDataSet.Tables.Add(resultDataTable);

        return resultDataSet;
    }

    [DataObjectMethod(DataObjectMethodType.Update)]
    public static int updateRental(int invoiceNumber, string vinId, int customerId, DateTime issueDate, DateTime returnDate, Decimal rentalFee)
    {
        string queryString = "sp_updateRental";
        SqlCommand selectCmd = new SqlCommand(queryString, new SqlConnection(ConfigurationManager.ConnectionStrings["QuickWheelzConnectionString"].ConnectionString));

        selectCmd.CommandType = CommandType.StoredProcedure;

        selectCmd.Parameters.AddWithValue("invoiceNumber", invoiceNumber);
        selectCmd.Parameters.AddWithValue("vinId", vinId);
        selectCmd.Parameters.AddWithValue("customerId", customerId);
        selectCmd.Parameters.AddWithValue("issueDate", issueDate);
        selectCmd.Parameters.AddWithValue("returnDate", returnDate);
        selectCmd.Parameters.AddWithValue("rentalFee", rentalFee);

        selectCmd.Connection.Open();

        int result = selectCmd.ExecuteNonQuery();

        if (result == 1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    [DataObjectMethod(DataObjectMethodType.Insert)]
    public static int insertRental(int invoiceNumber, string vinId, int customerId, SqlDbType issueDate, SqlDbType returnDate, Decimal rentalFee)
    {
        string queryString = "sp_insertRental";
        SqlCommand selectCmd = new SqlCommand(queryString, new SqlConnection(ConfigurationManager.ConnectionStrings["QuickWheelzConnectionString"].ConnectionString));

        selectCmd.CommandType = CommandType.StoredProcedure;

        selectCmd.Parameters.AddWithValue("invoiceNumber", invoiceNumber);
        selectCmd.Parameters.AddWithValue("vinId", vinId);
        selectCmd.Parameters.AddWithValue("customerId", customerId);
        selectCmd.Parameters.AddWithValue("issueDate", issueDate);
        selectCmd.Parameters.AddWithValue("returnDate", returnDate);
        selectCmd.Parameters.AddWithValue("rentalFee", rentalFee);

        selectCmd.Connection.Open();

        int result = selectCmd.ExecuteNonQuery();

        if (result == 1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataSet getRentalInfo(string vinId)
    {
        string queryString = "sp_getVehicleRental";
        SqlCommand selectCmd = new SqlCommand(queryString, new SqlConnection(ConfigurationManager.ConnectionStrings["QuickWheelzConnectionString"].ConnectionString));

        selectCmd.CommandType = CommandType.StoredProcedure;

        selectCmd.Parameters.AddWithValue("vinId", vinId);

        selectCmd.Connection.Open();
        SqlDataReader resultSet;

        resultSet = selectCmd.ExecuteReader();

        DataSet resultDataSet = new DataSet();

        DataTable resultDataTable = new DataTable();
        DataReaderAdapter da = new DataReaderAdapter();

        da.FillFromReader(resultDataTable, resultSet);
        resultDataSet.Tables.Add(resultDataTable);

        return resultDataSet;
    }
}